import type {
  CreateCrudOptionsProps,
  CreateCrudOptionsRet,
  CrudExpose,
} from '@fast-crud/fast-crud';

import type { FormRulesExt } from '#/api';
import type { BaseRoleModel } from '#/api/basic/system/model/baseRoleModel';
import type { ActionEnum } from '#/enums/commonEnum';

import { ref } from 'vue';

import { useAccess } from '@vben/access';
import { $t } from '@vben/locales';

import { BaseRoleApi } from '#/api/basic/system/baseRole';
import { DictEnum } from '#/enums/commonEnum';
import { PermCode } from '#/enums/perm';
import {
  backendDict,
  deleteButton,
  STATE_CONSTANT_DICT,
} from '#/plugins/fast-crud/common';

const { hasPermission } = useAccess();
const permCode = PermCode.basic.system.baseRole;

export function createCrudOptions(
  props: CreateCrudOptionsProps,
): CreateCrudOptionsRet {
  const selectedRowKeys = ref<string[]>([]);

  const currentRow = ref<string>();

  const onCurrentRowChange = (row: BaseRoleModel.BaseRoleResultVO) => {
    currentRow.value = row.id;
    selectedRowKeys.value = [row.id as string];

    props.context.select(row);
  };
  return {
    crudOptions: {
      request: {
        pageRequest: BaseRoleApi.pageRequest,
        addRequest: BaseRoleApi.addRequest,
        editRequest: BaseRoleApi.editRequest,
        delRequest: BaseRoleApi.delRequest,
        infoRequest: BaseRoleApi.infoRequest,
      },
      search: { show: false },
      actionbar: {
        buttons: {
          add: { show: hasPermission(permCode.add) },
          ...deleteButton({
            crudExpose: props.crudExpose,
            selectedRowKeys,
            role: permCode.delete,
            removeFn: BaseRoleApi.remove,
          }),
        },
      },
      table: {
        striped: true,
        rowKey: 'id',
        rowSelection: {
          type: 'radio',
          selectedRowKeys,
          onChange: (changed: string[]) => {
            selectedRowKeys.value = changed;
          },
        },
        customRow: (row: BaseRoleModel.BaseRoleResultVO) => {
          const clazz = row.id === currentRow.value ? 'fs-current-row' : '';
          return {
            style: 'cursor: pointer;',
            onClick() {
              onCurrentRowChange(row);
            },
            class: clazz,
          };
        },
      },
      rowHandle: {
        width: '120px',
        fixed: 'right',
        buttons: {
          edit: { dropdown: true, show: hasPermission(permCode.edit) },
          bindRole: {
            show: hasPermission(permCode.bindUser),
            type: 'link',
            text: '绑定',
            size: 'small',
            click(context) {
              props.context.bindEmployeeRef.value.setData({
                id: context.row.id,
              });
              props.context.bindEmployeeRef.value.open();
            },
          },
          view: { dropdown: true, text: '查看' },
          remove: { show: hasPermission(permCode.delete), dropdown: true },
          copy: { show: hasPermission(permCode.add), dropdown: true },
        },
      },
      columns: {
        code: {
          title: $t('basic.system.baseRole.code'),
          type: 'text',
          search: { show: true },
          column: { show: false },
        },
        name: {
          title: $t('basic.system.baseRole.name'),
          type: 'text',
          search: { show: true },
        },
        category: {
          title: $t('basic.system.baseRole.category'),
          type: 'dict-radio',
          dict: backendDict(DictEnum.RoleCategoryEnum),
          search: { show: true },
          addForm: { value: '10' },
          column: { width: 80 },
        },
        state: {
          title: $t('basic.system.baseRole.state'),
          type: 'dict-radio',
          dict: STATE_CONSTANT_DICT,
          column: { width: 60 },
          search: { show: true },
        },
        remarks: {
          title: $t('basic.system.baseRole.remarks'),
          type: 'textarea',
          column: { show: false },
        },
      },
    },
  };
}

export const frontRules = (
  _crudExpose: CrudExpose,
  _mode: ActionEnum,
): FormRulesExt => {
  return {};
};
